package com.example.securitytest.controller;

import com.example.securitytest.domain.ResponseResult;
import com.example.securitytest.domain.User;
import com.example.securitytest.service.LoginService;
import com.example.securitytest.service.impl.userService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class LoginController {

	@Autowired
	private LoginService loginService;

	@Autowired
	private userService userService;

	@Autowired
	private PasswordEncoder passwordEncoder;

	//注册
	@PostMapping("/register")
	public ResponseResult register(@RequestBody User user){
		String password = user.getPassword();
		BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
		String encode = passwordEncoder.encode(password);
		user.setPassword(encode);
		//默认为用户
		//user.setRole("1");
		//注册即是正常账户
		user.setStatus("1");
		userService.register(user);
		return new ResponseResult<>(200,"注册成功",null);
	}

	//登录
	@RequestMapping("/login")
	public ResponseResult login(@RequestBody User user, HttpServletRequest request) {
		String userAgent = request.getHeader("User-Agent");
		return loginService.login(user, userAgent);
	}

	//退出登录
	@RequestMapping("/logout")
	public ResponseResult logout(){
		return loginService.logout();
	}



}
